var express = require('express');
var router = express.Router();
const mongodb= require('../components/mongo');

const multer = require('multer');
const uploader = multer({dest:'uploads/'});

/* GET home page. */
router.get('/',async function(req, res, next) {
   console.log(req.query.u_id);
   var m_uid = req.query.u_id;
   if(m_uid==undefined){
	   //don't' have id
	   res.json({	   
		       errno: 0,
		       errmsg: "",
		       data: {
		           res: false
		       }
	   })
   }else{
	   //one article containers many supports
	   const result = await mongodb.find("Article",{u_id:m_uid});
	   console.log(result);
	   var supports = [];
		if(result.length > 0){
			for(item in result){
				for(supports_item in result[item].supports){
					if( result[item].supports[supports_item].u_id!=m_uid){
						var obj = {
							a_id : result[item].a_id,
							u_id : result[item].supports[supports_item].u_id,
							raw_content : result[item].content.substring(0,5),
							time_stamp :  result[item].supports[supports_item].time_stamp,
						}
						supports.push(obj);
					}
				}
			}
			res.json({
					   
					       errno: 0,
					       errmsg: "",
					       data: {
					           res: (supports.length > 0) ? true : false,
							   supports:supports
					       }
					   
			})
		}else{
			res.json({
					   
					       errno: 0,
					       errmsg: "",
					       data: {
					           res: false
					       }
					   
			})	
		}
	   
   }

});


module.exports = router;